package com.hejia.alauda.entity;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;

import com.hejia.alauda.entity.example.RedpacketExample;
import com.hejia.alauda.persistence.mybatis.RedpacketMapper;
import com.hejia.alauda.service.CommonServiceImpl;
import com.hejia.alauda.service.UserService;
import com.hejia.alauda.utils.Encryption;
import com.hejia.alauda.utils.RateUtil;
import com.hejia.alauda.utils.Utils;

public class BonusRecord extends BaseEntity {

	public final static String STATUS_UNUSED = "未使用";
	public final static String STATUS_USED = "已使用";
	public final static String STATUS_EXPIRED = "已过期";

	/**
	 * 
	 */
	private static final long serialVersionUID = 808301186863861400L;

	@Column
	private BigDecimal amount; // 红包金额

	@Column
	private Date expiredDate; // 红包过期时间

	@Column
	private String description;// 具体描述

	@Column
	private String type; // 红包类型或者红包用途

	@Column
	private String code; // 红包编码

	@Column
	private User user; // 用户

	@Column
	private Boolean consume;// 是否已经消费

	@Column
	private Date consumeDate;// 消费时间

	@Column
	private BigDecimal remaining; // 剩余金额

	@Column
	private String related; // 相关联的数据 一般放用户ID

	@Column
	private Boolean read; // 是否已读

	private String name; // 红包名称

	private String instruction; // 红包使用说明

	private String background; // 背景图片

	private String productName; // 激活红包的产品名称

	private Long user_id;

	private Redpacket redpacket = null;

	@Column
	private Boolean sevenDayExpirePushAlerted; // 􏱶􏰷􏱶􏰷􏱶􏲦􏱄􏰜􏳑红包7天后到期是否已推送消息提醒

	@Column
	private Boolean oneDayExpirePushAlerted; // 􏱶􏰷􏱶􏰷􏱶􏲦􏱄􏰜􏳑红包1天后到期是否已推送消息提醒

	@Column
	private Boolean oneDayExpireSmsAlerted; // 􏱶􏰷􏱶􏰷􏱶􏲦􏱄􏰜􏳑红包1天后到期是否已短信提醒

	@Column
	private String operator; // 操作人

	public Long getUser_id() {
		return user_id;
	}

	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public Date getExpiredDate() {
		return expiredDate;
	}

	public void setExpiredDate(Date expiredDate) {
		this.expiredDate = expiredDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Boolean getConsume() {
		return consume;
	}

	public void setConsume(Boolean consume) {
		this.consume = consume;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BigDecimal getRemaining() {
		return remaining;
	}

	public void setRemaining(BigDecimal remaining) {
		this.remaining = remaining;
	}

	public Date getConsumeDate() {
		return consumeDate;
	}

	public void setConsumeDate(Date consumeDate) {
		this.consumeDate = consumeDate;
	}

	public String getRelated() {
		return related;
	}

	public void setRelated(String related) {
		this.related = related;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getInstruction() {
		return instruction;
	}

	public void setInstruction(String instruction) {
		this.instruction = instruction;
	}

	public String getBackground() {
		return background;
	}

	public void setBackground(String background) {
		this.background = background;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getStatus() {
		return this.consume ? STATUS_USED : (Utils.formatDate(new Date())
				.compareTo(Utils.formatDate(expiredDate)) > 0 ? STATUS_EXPIRED
				: STATUS_UNUSED);
	}

	public Integer getRemainingDays() {
		int days = RateUtil.calculateDate(new Date(), getExpiredDate());
		return days >= 0 ? days + 1 : 0;
	}

	/**
	 * 有效期限，单位天
	 * 
	 * @return
	 */
	public Integer getValidPeriod() {
		int days = RateUtil.calculateDate(this.getCreateDate(),
				getExpiredDate());
		return days >= 0 ? days + 1 : 0;
	}

	public String getIntroduction() {
		/* 如果是邀请红包显示邀请人信息 */
		if ("INVITE".equalsIgnoreCase(trimToEmpty(getRedpacket().getCode()))) {
			try {
				if (getRelated() != null) {
					Long id = new Long(getRelated());
					UserService userService = CommonServiceImpl.applicationContext
							.getBean(UserService.class);
					User user = userService.findUserById(id);
					if (isNotBlank(user.getRealName())) {
						return "来自 "
								+ Encryption.obfuscatedName(user.getRealName());
					}
					if (isNotBlank(user.getMobile())) {
						return "来自 "
								+ Encryption.obfuscatedMobile(user.getMobile());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return getRedpacket().getIntroduction();
	}

	public Redpacket getRedpacket() {
		// Redpacket redpacket = null;
		if (redpacket == null && isNotBlank(getCode())) {
			RedpacketMapper redpacketMapper = CommonServiceImpl.applicationContext
					.getBean(RedpacketMapper.class);
			RedpacketExample example = new RedpacketExample();
			example.appendCriterion("code=", getCode());
			redpacket = redpacketMapper.selectOneByExample(example);
		}
		if (redpacket == null)
			redpacket = new Redpacket();
		return redpacket;
	}

	public Boolean getRead() {
		return read;
	}

	public void setRead(Boolean read) {
		this.read = read;
	}

	/**
	 * 提现额度
	 * 
	 * @return
	 */
	public Integer getWithdrawLimit() {

		Redpacket redpacket = this.getRedpacket();
		if (redpacket == null) {
			return 0;
		}

		return redpacket.getWithdrawLimit();
	}

	public Boolean getSevenDayExpirePushAlerted() {
		return sevenDayExpirePushAlerted;
	}

	public void setSevenDayExpirePushAlerted(Boolean sevenDayExpirePushAlerted) {
		this.sevenDayExpirePushAlerted = sevenDayExpirePushAlerted;
	}

	public Boolean getOneDayExpirePushAlerted() {
		return oneDayExpirePushAlerted;
	}

	public void setOneDayExpirePushAlerted(Boolean oneDayExpirePushAlerted) {
		this.oneDayExpirePushAlerted = oneDayExpirePushAlerted;
	}

	public Boolean getOneDayExpireSmsAlerted() {
		return oneDayExpireSmsAlerted;
	}

	public void setOneDayExpireSmsAlerted(Boolean oneDayExpireSmsAlerted) {
		this.oneDayExpireSmsAlerted = oneDayExpireSmsAlerted;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

}
